package com.tvein.sanguo.view
{
	//flash
	import flash.events.MouseEvent;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	//pureMVC
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.utilities.fabrication.patterns.mediator.FlashMediator;	
	//custom
	import com.tvein.sanguo.common.CommonConstants;
	import com.tvein.SanGuo;
//
	public class ApplicationMediator extends FlashMediator
	{
		public static const NAME:String = 'ApplicationMediator';
		
		public function ApplicationMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		
		public function get sanguo():SanGuo
		{
			return viewComponent as SanGuo;
		}
		
		//Custom
		public var sysLog:TextField = new TextField();
		
		override public function onRegister() : void
        {
			sanguo.stage.addEventListener(KeyboardEvent.KEY_UP, _keyDownHandler);
			sanguo.stage.addEventListener(MouseEvent.RIGHT_CLICK, _onRightClick);
			
			sysLog.name = "SystemLog";
			sysLog.background = true;
			sysLog.backgroundColor = 0xffffff;
			sysLog.textColor = 0x000000;
			sysLog.selectable = false;
			sysLog.wordWrap = true;
			
			sysLog.x = 0;
			sysLog.y = 0;
			sysLog.width = sanguo.stage.stageWidth;
			sysLog.height = sanguo.stage.stageHeight * .4;
			
			sanguo.addChild(sysLog);
			sysLog.appendText("Game Server:" + CommonConstants.GAME_SERVER + "\n");
			sysLog.appendText("System Initializing...\n");
		}
		
		private function _onRightClick(evt:MouseEvent):void
		{
			
		}
		
		private function _keyDownHandler(evt:KeyboardEvent):void
		{
			sysLog.width = 0;
			sysLog.height = 0;
			
			var maxLine:int = sysLog.height * .06;
			
			if (sysLog.numLines > maxLine)
			{
				var index:int = 0;
				for (var i:int = 0; i < sysLog.numLines - maxLine; i++)
				{
					index += sysLog.getLineLength(i);
				}
				var str:String = sysLog.text.slice(index);
				sysLog.text = str;
			}
			
			switch(evt.keyCode)
			{
				case 192:	// case "'"
					sendNotification(CommonConstants.SYSTEM_CONSOLE, ["-console", sanguo.getChildIndex(sysLog)]);
					break;
			}
		}
	}
}